package com.hzdp.coc;

/**
 * 3个线程
 * 
 * a线程打印A,b线程打印B,c线程打印C
 * 
 * 要求打印10遍ABC
 * 
 * @author liuming
 * 
 */
public class Printer {

	public static void main(String[] args) throws InterruptedException {
		Object a = new Object();
		Object b = new Object();
		Object c = new Object();

		PrinterThread pa = new PrinterThread(a, c, "a");
		PrinterThread pb = new PrinterThread(b, a, "b");
		PrinterThread pc = new PrinterThread(c, b, "c");

		synchronized (a) {
			synchronized (b) {
				synchronized (c) {
				}
			}
		}
		pa.start();
		Thread.sleep(1);
		pb.start();
		Thread.sleep(1);
		pc.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class PrinterThread extends Thread {
	private Object self;
	private Object pre;
	private String text;

	public PrinterThread(Object self, Object pre, String text) {
		this.self = self;
		this.pre = pre;
		this.text = text;
	}

	public PrinterThread(String text) {
		this.text = text;
	}

	@Override
	public void run() {
		synchronized (pre) {
			synchronized (self) {
				System.out.println(text);
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
